var int;
var hour=0;
var minute=0;
var second=0;
function setDate() {
    var now=new Date();
    var day = ("0" + now.getDate()).slice(-2);
    var month = ("0" + (now.getMonth() + 1)).slice(-2);
    var MyDate = now.getFullYear()+"-"+(month)+"-"+(day) ;
    document.getElementById("ttdate").value=MyDate;
    init();
}
function init(){
    document.getElementById("whichDate").value=document.getElementById("ttdate").value;
    var nowDate=document.getElementById("whichDate").value;
    getWinnerId();
    ajaxCall("GET",path+"/QuestionServlet?method=getContentByDate&date="+nowDate,setQuestion);
    ajaxCall("GET",path+"/TeamExamTransactionServlet?method=getGroupMessage&date="+nowDate,setGroups);
}
function setWinners() {
    var date=document.getElementById("ttdate").value;
    var table=document.getElementById("t1");
    var maxScore=table.rows[1].cells[3].innerHTML;
    var winnerGroupId=table.rows[1].cells[0].innerHTML;
    for(var i=1;i<table.rows.length;i++){
        if(maxScore<table.rows[i].cells[3].innerHTML){
            maxScore=table.rows[i].cells[3].innerHTML;
            winnerGroupId=table.rows[i].cells[0].innerHTML;
        }
    }
    ajaxCall("POST",path+"/TeamExamServlet?method=setWinners&date="+date+"&winnerGroupId="+winnerGroupId,setWinnersResult);
}

function setGroups(res) {
    var obj=JSON.parse(res);
    var table=document.getElementById("t1");
    clearTable("t1");
    for(var i in obj){
        var newRow=table.insertRow(1);
        var groupId=newRow.insertCell(0);
        var userNames=newRow.insertCell(1);
        var spendTime=newRow.insertCell(2);
        var score=newRow.insertCell(3);
        var setScore=newRow.insertCell(4);

        groupId.innerHTML=obj[i].groupId;
        userNames.innerHTML=obj[i].userNames;
        spendTime.innerHTML=obj[i].spendTime;
        if(obj[i].score==0) {
            score.innerHTML="未批改";
            setScore.innerHTML = "<input type='button' value='批改' onclick='setScore(this)'/>";
        }
        else{
            if(obj[i].score==5){
                document.getElementById("top").value=1;
            }
            score.innerHTML=obj[i].score;
            setScore.innerHTML = "<input type='button' value='查看' onclick='showWork(this)'/>";
        }
    }
}
function initt(){
    var nd = new Date();
    nd.getHours();
    nd.getMinutes();
    nd.getSeconds();
    var nowDate = nd.toLocaleDateString();
    ajaxCall("GET",path+"/TeamExamTransactionServlet?method=checkTodaySubmit&date="+nowDate,checkTodayResult);
}
function checkTodayResult(res) {
    var obj=JSON.parse(res);
    if(obj){
        var div=document.getElementById("divMessage");
        div.style.display="block";
        init_s();
    }
    else{
        init_s();
    }
}
function clearTable(tableName) {
    var tb = document.getElementById(tableName);
    var rowNum = tb.rows.length;
    for (i = 1; i < rowNum; i++) {
        tb.deleteRow(i);
        rowNum = rowNum - 1;
        i = i - 1;
    }
}

function initToday() {
    ajaxCall("GET",path+"/GroupServlet?method=getGroupNames",getGroupNamesResult);
    ajaxCall("GET",path+"/GroupServlet?method=checkGrouped",checkGroupedResult);
}
function getGroupNamesResult(res) {
    var obj=JSON.parse(res);
    var userNames=document.getElementById("userNames");
    userNames.innerText=obj;
}

function checkGroupedResult(res){
    var obj=JSON.parse(res);
    if(obj==0){
        alert("该学员没有分组，无法开始结对！");
    }
    else{
       initt();
    }
}

function init_s() {
        var nd = new Date();
        nd.getHours();
        nd.getMinutes();
        nd.getSeconds();
        var nowDate = nd.toLocaleDateString();
        ajaxCall("GET", path + "/QuestionServlet?method=getContentByDate&date=" + nowDate, setQuestion);
}

function showWork(row){
    var rowIndex=row.parentNode.parentNode.rowIndex;
    var table=document.getElementById("t1");
    var groupId=table.rows[rowIndex].cells[0].innerHTML;
    var score=table.rows[rowIndex].cells[3].innerHTML;
    var Dtime=document.getElementById("whichDate").value;
    var top=document.getElementById("top").value;
    window.location.href="showGroupWork.jsp?Dtime="+Dtime+"&groupId="+groupId+"&top="+top+"&score="+score;
}

function setScore(row){
    var rowIndex=row.parentNode.parentNode.rowIndex;
    var table=document.getElementById("t1");
    var groupId=table.rows[rowIndex].cells[0].innerHTML;
    var score=table.rows[rowIndex].cells[3].innerHTML;
    var Dtime=document.getElementById("whichDate").value;
    var top=document.getElementById("top").value;
    window.location.href="setGroupScore.jsp?Dtime="+Dtime+"&groupId="+groupId+"&top="+top+"&score="+score;
}
function setWinnersResult(res) {
    var obj=JSON.parse(res);
    if(obj){
        alert("set winner success!");
        getWinnerId();
    }
    else{
        alert("set winner failed!");
    }
}
function getWinnerId() {
    var nowDate=document.getElementById("whichDate").value;
    ajaxCall("GET",path+"/TeamExamServlet?date="+nowDate,getWinnerIdResult);
}

function getWinnerIdResult(res) {
    var obj=JSON.parse(res);
    if(obj==null){
        document.getElementById("winnerId").value="空";
    }
    else{
        document.getElementById("winnerId").value=obj[0]+"、"+obj[1];
    }
}

function stop(){
    alert("已用时2小时，请提交作业！");
    window.clearInterval(int);
}
function time_start() {
    int=setInterval(timer,1000);
}
function getBeginTimeResult(res) {
    var obj=JSON.parse(res);
    if(obj!=null||obj!="") {
        var str = obj.toString();
        str = str.replace("/-/g", "/");
//// str =  str.replace("T"," ");
        var beginTime = new Date(str);

        var nowTime = new Date();
        var beginSeconds = beginTime.getHours() * 60 * 60;
        beginSeconds += beginTime.getMinutes() * 60;
        beginSeconds += beginTime.getSeconds();

        var endSeconds = (nowTime.getHours()) * 60 * 60;
        endSeconds += (nowTime.getMinutes()) * 60;
        endSeconds += nowTime.getSeconds();

        var spendSeconds = endSeconds - beginSeconds;
        hour = parseInt(spendSeconds / 3600);
        minute = parseInt((spendSeconds - hour * 3600) / 60);
        second = spendSeconds - hour * 3600 - minute * 60;
    }
}

function timer(){
    second=second+1;
    if(second>=60)
    {
        second=0;
        minute=minute+1;
    }

    if(minute>=60)
    {
        minute=0;
        hour=hour+1;
    }
    document.getElementById("date").innerHTML="用时："+hour+'时'+minute+'分'+second+'秒';
    if(hour==2){
        stop();
    }
}
function setBeginTimeResult(res) {
    var obj=JSON.parse(res);
    if(obj){
        alert("开始今日结对作业!");
    }
    else{
        alert("今日结对开始失败!");
    }
}


function historyDetails(){
    var date=document.getElementById("historydate").innerText;
    ajaxCall("GET",path+"/QuestionServlet?method=getContentByDate&date="+date,setHistoryQuestion);
}

function setHistoryQuestion(res) {
    var obj = JSON.parse(res);
    var historyQuestion=document.getElementById("historyQuestion");
    historyQuestion.value =obj;
}

function setQuestion(res){
    var obj = JSON.parse(res);
    var tquestion=document.getElementById("tquestion");
    tquestion.value =obj;
    if(obj!="今日没有作业！"){
        var div=document.getElementById("divSub");
        div.style.display="block";
        var nd = new Date();
        var nowDate = nd.toLocaleDateString();
        ajaxCall("GET",path+"/TeamExamTransactionServlet?method=getBeginTime&date="+nowDate,getBeginTimeResult);
        time_start();
        checkClick();
    }

}
function checkClick() {
    var nd = new Date();
    var nowDate = nd.toLocaleDateString();
    ajaxCall("GET",path+"/TeamExamTransactionServlet?method=checkToday&date="+nowDate,checkClickResult);
}
function checkClickResult(res) {
    var obj=JSON.parse(res);
    var nd = new Date();
    var nowDate = nd.toLocaleDateString();
    var nDate = nd.toLocaleDateString() + " " + nd.getHours() + ":" + nd.getMinutes() + ":" + nd.getSeconds();
    if(!obj){
        ajaxCall("GET", path + "/TeamExamTransactionServlet?method=setBeginTime&startTime=" + nDate+"&date="+nowDate, setBeginTimeResult);
    }
}



